package com.yjt.sousou.utils;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.view.View;

import androidx.core.content.ContextCompat;
import androidx.recyclerview.widget.RecyclerView;

import com.yjt.sousou.R;


/**
 * @author chs
 * date：2019-04-24 17:05
 * des： RecyclerView分割线
 */
public class RecyclerViewItemDecoration extends RecyclerView.ItemDecoration {

    /** 分割线高度*/
    private int mDividerHeight;
    /** 分割线Drawable*/
    private Drawable mDivider;

    /**
     * 是否绘制最后一条
     */
    private boolean isDrawLast = true;

    public RecyclerViewItemDecoration(Context context) {
        mDividerHeight = context.getResources().getDimensionPixelSize(R.dimen.dp_10);
    }

    public RecyclerViewItemDecoration(Context context, int dividerHeight, Drawable divider) {
        if (divider == null) {
            mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
        } else {
            mDivider = divider;
        }
        mDividerHeight = dividerHeight;
    }
    public RecyclerViewItemDecoration(Context context, int dividerHeight, Drawable divider, boolean isDrawLast) {
        if (divider == null) {
            mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
        } else {
            mDivider = divider;
        }
        this.isDrawLast = isDrawLast;
        mDividerHeight = dividerHeight;
    }
    public RecyclerViewItemDecoration(Context context, int dividerHeight, boolean isDrawLast) {
            mDivider = ContextCompat.getDrawable(context, R.drawable.line_divider);
            mDividerHeight = dividerHeight;
            this.isDrawLast = isDrawLast;
    }

    @Override
    public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
        super.getItemOffsets(outRect, view, parent, state);
        //当前条目的position
        int pos = parent.getChildLayoutPosition(view);
        if(pos == 0){
            outRect.top = mDividerHeight;
        }
        outRect.bottom = mDividerHeight;

    }

    @Override
    public void onDrawOver(Canvas c, RecyclerView parent, RecyclerView.State state) {
        if(mDivider!=null){
            int left = parent.getPaddingLeft();
            int right = parent.getWidth() - parent.getPaddingRight();

            int childCount;
            if(isDrawLast){
                childCount = parent.getChildCount();
            }else {
                childCount = parent.getChildCount()-1;
            }
            for (int i = 0; i < childCount; i++) {
                View child = parent.getChildAt(i);

                RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child.getLayoutParams();

                int top = child.getBottom() + params.bottomMargin;
                int bottom = top + mDividerHeight;
                mDivider.setBounds(left, top, right, bottom);
                mDivider.draw(c);
            }
        }
    }
}
